﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;

namespace KeyboardBubble
{
    public partial class KeyMessageBox : Form
    {
        public System.Windows.Forms.Timer fadeOutTimer;

        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        private static extern IntPtr CreateRoundRectRgn
        (
            int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse
        );

        public KeyMessageBox(string key, string note = "")
        {
            InitializeComponent();
            // 计算文本长度
            Font keyFont = new Font("微软雅黑", 16);
            SizeF keySize = TextRenderer.MeasureText(key, keyFont);
            Font noteFont = new Font("微软雅黑", 10);
            SizeF noteSize = TextRenderer.MeasureText(note, noteFont);
            var keyWidth = (int)keySize.Width;
            var noteWidth = (int)noteSize.Width;
            var maxWidth = keyWidth > noteWidth ? keyWidth : noteWidth;
            // 设置文本显示
            this.labKey.Text = key;
            this.lableNote.Text = note;
            // 计算窗体实际宽高
            int height = 36 + 5; // 标题豪赌+预留高度
            if (!string.IsNullOrWhiteSpace(note))
            {
                height += 26 - 3; // 内容高度-重叠高度
            }
            this.Height = height;
            this.Width = maxWidth + 40;
            // 设置圆角大小
            Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 25, 25));
            // 设置窗体在屏幕右下角
            this.StartPosition = FormStartPosition.Manual;
            this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width - 30,
                                      Screen.PrimaryScreen.WorkingArea.Height - this.Height - 30);
            // 创建并启动计时器，一定时间后开始淡出
            fadeOutTimer = new System.Windows.Forms.Timer();
            fadeOutTimer.Interval = 3000; // 设置间隔为3000毫秒（3秒）
            fadeOutTimer.Tick += new EventHandler(StartFadeOut);
            fadeOutTimer.Start();
        }
        public void StartFadeOut(object sender, EventArgs e)
        {
            // 停止计时器
            fadeOutTimer.Stop();

            // 开始淡出效果
            Timer fadeTimer = new Timer();
            fadeTimer.Interval = 33; // 设置淡出速度
            fadeTimer.Tick += new EventHandler(FadeOut);
            fadeTimer.Start();
        }
        public void FadeOut(object sender, EventArgs e)
        {
            // 逐渐减少窗体的透明度
            if (this.Opacity > 0)
            {
                this.Opacity -= 100 / 1000.0;
            }
            else
            {
                // 当透明度降到0时关闭窗体
                ((Timer)sender).Stop();
                this.Close();
                WinManager.Instance.removeObject(this);
            }
        }

        public void SetBottom(int bottom)
        {
            Point p = this.Location;
            this.Location = new Point(p.X, Screen.PrimaryScreen.WorkingArea.Height - bottom);
        }
    }
}
